import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});
  @override
  _LoginPageState createState() => _LoginPageState();
  // @override
  // Widget build(BuildContext context) {
  //   return Scaffold(
  //     appBar: AppBar(title: Text("登陆")),
  //     body: Center(child: Column(children: [Text("登陆页面")])),
  //   );
  // }
}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  final _accountController = TextEditingController();
  final _passwordController = TextEditingController();
  final bool _isLoading = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [_buildLogo(), SizedBox(height: 46), _buildForm()],
            ),
          ),
        ),
      ),
    );
  }

  // 表单
  Widget _buildForm() {
    return Form(
      key: _formKey,
      child: Column(
        children: [
          _buildAccount(),
          SizedBox(height: 16),
          _buildPasswordField(),
          SizedBox(height: 24),
          _buildLoginButton(),
        ],
      ),
    );
  }

  Widget _buildAccount() {
    return TextFormField(
      controller: _accountController,
      decoration: InputDecoration(
        label: Text("账号"),
        prefixIcon: Icon(Icons.account_balance),
        border: OutlineInputBorder(),
      ),
      keyboardType: TextInputType.text,
      validator: (value) {
        if (value == null || value.isEmpty) {
          return "请输入账号";
        } else if (value.length <= 5) {
          return "账号位数必须大于6位";
        }
        return null;
      },
    );
  }

  Widget _buildPasswordField() {
    return TextFormField(
      controller: _passwordController,
      decoration: InputDecoration(
        labelText: '密码',
        prefixIcon: Icon(Icons.lock),
        border: OutlineInputBorder(),
      ),
      obscureText: true,
      validator: (value) {
        if (value == null || value.isEmpty) {
          return '请输入密码';
        }
        if (value.length < 6) {
          return '密码长度至少6位';
        }
        return null;
      },
    );
  }

  Widget _buildLoginButton() {
    return SizedBox(
      width: double.infinity,
      child: ElevatedButton(
        onPressed: _isLoading ? null : _handleLogin,
        child:
            _isLoading
                ? CircularProgressIndicator(color: Colors.white)
                : Text('登录'),
      ),
    );
  }

  Widget _buildLogo() {
    return Container(
      padding: EdgeInsets.all(0),
      decoration: BoxDecoration(
        color: Colors.white,
        shape: BoxShape.rectangle,
        boxShadow: [
          BoxShadow(color: Colors.black12, blurRadius: 10, spreadRadius: 2),
        ],
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: Image.asset(
        'assets/logo.png', // 你的logo路径
        height: 100,
        width: 100,
      ),
    );
  }

  Future<void> _handleLogin() async {}
}
